package com.syxp.pda.common.interceptor;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;

/**
 * 登录验证拦截器
 */
public class AuthorityInterceptor extends MethodFilterInterceptor{

	@Override
	protected String doIntercept(ActionInvocation actioninvocation) throws Exception {
		Object useraccount = actioninvocation.getInvocationContext().getSession().get("useraccount");//ServletActionContext.getRequest().getSession().getAttribute("useraccount");
		if(useraccount != null){
			return actioninvocation.invoke(); //递归调用拦截器
		}else{
			// 拦截器的处理：     
			HttpServletResponse response = ServletActionContext.getResponse(); 
			HttpServletRequest request = ServletActionContext.getRequest(); 
			if (request.getHeader("x-requested-with") != null    
				&& request.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")) {     
			    response.addHeader("sessionstatus", "timeout");  
			}
			return Action.LOGIN; //返回到登录页面
		}
	}
}

